﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using Entity;

public partial class admin_product_reviews : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindReviews(String.IsNullOrEmpty(e.Sort) ? "ReviewDate" : e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    protected void imgSearchButton_Click(object sender, EventArgs e)
    {

    }

    private void BindReviews(string sort, int start, int limit)
    {
        ECommerce.DAL.CustomCollection<TblProductReview> data = null;
        int totalCount = 0;

        if (this._useSearch)
        {
            Session["_useSearch"] = null;
        }
        else
        {
            data = TblProductReview.GetAllProductReviewsWithProductPaged(sort, start, limit);
            totalCount = TblProductReview.GetAllProductReviewsWithProductPagedCount();
        }

        Store1.DataSource = data;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void UpdateReview(int id, string CustomerName, string CustomerEmail, decimal RatingValue, bool IsApproved)
    {
        TblProductReview review = TblProductReview.GetTblProductReviewByProductReviewId(id);
        review.CustomerEmail = CustomerEmail;
        review.CustomerName = CustomerName;
        review.RatingValue = RatingValue;
        review.IsApproved = IsApproved;
        review.Save();

        Email.SendGenericEmail(review.CustomerName, review.CustomerEmail, "Review Status Updated", String.Format("Your product review for the product <strong>{0}</strong> has been {1} by the administrator.", review.ProductName, review.IsApproved.Value ? "published" : "declined"));

        //Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void DeleteReviews(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        TblProductReview.DeleteTblProductReviewList(idList);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void SearchCustomer()
    {
        _useSearch = true;
        Store1.DataBind();
    }
}